home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / NewDisplay-c / NewDisplay.c next >
Encoding:
Text File  |  1994-12-04  |  10.7 KB  |  415 lines  |  [TEXT/MMCC]

  1.  
  2. //• Subject: Display a file program
  3. //• Message-ID: <1701@uw-beaver>
  4. //• Date: Wed, 6 Nov 85 22:18:16 MST
  5. //• Date-Received: Fri, 8 Nov 85 02:05:35 MST
  6. //• Sender: daemon@uw-beaver
  7. //• Organization: U of Washington Computer Science
  8. //• Lines: 372
  9.  
  10. //• From: kangaro!milo@Purdue.EDU
  11.  
  12. //• This is a little program I whipped up in Megamax C that will 
  13. //• display any Mac file of type TEXT.  It is very handy for looking 
  14. //• at text files you have downloaded from a BBS without having to 
  15. //• load a text editor.  The program loads in about 4 seconds and 
  16. //• supports standard Mac desk accessorys...etc.
  17.  
  18. //• Let me know if you have any problems with the program...the 
  19. //• source follows...
  20.  
  21. //• Greg Corson
  22. //• UUCP: {ihnp4 | ucbvax}!pur-ee!kangaro!milo
  23. //• ARPA: pur-ee!kangaro!milo@Purdue.ARPA
  24. //• EDU: kangaro!milo@ee.Purdue.EDU
  25. //• Or call my BBS at (219) 277-5825
  26.  
  27. //• This sample program allows a user to open a file and print 
  28. //• it's contents to a window on the Mac screen.  To give the 
  29. //• compiled program an Icon copy the resources from Display.rsrc 
  30. //• (use Rmover or Resource Editor) and paste them into the 
  31. //• compiled file.  Then use SetFile to change the creator name of 
  32. //• the compiled file to DFIL and set the bundle bit.  When you 
  33. //• return to the finder the program icon should have an icon.
  34.  
  35. //• By Greg Corson
  36. //• 19141 Summers Drive
  37. //• South Bend, IN 46637
  38. //• (219) 272-2136
  39. //• UUCP: {ihnp4 | ucbvax}!pur-ee!kangaro!milo
  40. //• ARPA: pur-ee!kangaro!milo@Purdue.ARPA
  41. //• EDU: kangaro!milo@ee.Purdue.EDU
  42. //• Or call my BBS at (219) 277-5825
  43.  
  44. #include <packages.h>
  45.  
  46. #define lastMenu      4
  47. #define appleMenu      1
  48. #define fileMenu    256
  49. #define editMenu    257
  50. #define stopMenu    258
  51.  
  52. RgnHandle        updateRgn;
  53. MenuHandle    myMenus[lastMenu+1];
  54. Rect            screenRect, dragRect, pRect;
  55. Boolean            doneFlag, temp;
  56. EventRecord        myEvent;
  57. short                code, refNum;
  58. WindowRecord    wRecord;
  59. WindowPtr        myWindow, whichWindow;
  60. GrafPtr            temPort;
  61. short                theMenu, theItem;
  62. short                fileOpen, howWide, fd1;
  63. long            count;
  64. char            tempBuf[32];
  65.  
  66. void Center (char *str);
  67.  
  68. //•---------------------------------------------------------------------------*/
  69. //• This subroutine processes commands from the menu bar, where
  70. //• theMenu is the menu ID, theItem  is the item number in the menu.
  71.  
  72. void DoCommand (long mSelect)
  73. {
  74.     short theMenu = HiWord(mSelect);
  75.     short theItem = LoWord(mSelect);
  76.     char name[256];
  77.     SFReply rep;
  78.     SFTypeList typeList;
  79.     short i;
  80.  
  81.     Point openP = { 100, 60 };
  82.  
  83.     //• Switch to decide what menu the cursor is in.
  84.     switch (theMenu)
  85.     {
  86.         //• Mouse down in apple menu.
  87.         case appleMenu:    
  88.             //• Item one is the "about Display a file" box.
  89.             if (theItem == 1)
  90.             {
  91.                 TextFont (systemFont);
  92.                 TextSize (12);
  93.                 EraseRect (&pRect);
  94.                 MoveTo (pRect.left, pRect.top+70);
  95.                 Center ((char*) "\pDisplay a file program");
  96.                 Center ((char*) "\pCopyright 1985 by Greg Corson");
  97.                 Center ((char*) "\pKangaroo Koncepts, Inc.");
  98.                 Center ((char*) "\p19141 Summers Drive");
  99.                 Center ((char*) "\pSouth Bend, IN 46637");
  100.                 Center ((char*) "\p1(219) 277-5306");
  101.                 TextFont (monaco);
  102.                     TextSize (9);
  103.                 Move (0, -3);
  104.                 Center ((char*) "\pFeel free to give this program away to all your friends.");
  105.                 Center ((char*) "\pIt should NOT be sold for profit.  Be sure to try our");
  106.                 Center ((char*) "\pComputer Based Communications System \"The Connection\"");
  107.                 Center ((char*) "\pFree demo line (219) 277-5825 available 24 hours at 300 or");
  108.                 Center ((char*) "\p1200 baud.  Be sure to look at the \"MacTech\" special");
  109.                 Center ((char*) "\pinterest group for information of interest to Mac");
  110.                 Center ((char*) "\pprogrammers and the \"macintosh\" SIG for general info.");
  111.                 Center ((char*) "\pAnother old program \"brought back from the dead\"");
  112.                 Center ((char*) "\pby Ken Long, at itty bitty bytes™");
  113.                 Pretty ();
  114.                 MoveTo (pRect.left + 1, pRect.bottom - 2);
  115.             }
  116.             //• The rest of the items are desk accessorys.
  117.             else
  118.                 {
  119.                     GetItem (myMenus[1], theItem, (StringPtr) name);
  120.                     refNum = OpenDeskAcc ((StringPtr) name);
  121.                     SetPort (myWindow);
  122.             }
  123.         break;
  124.         
  125.         case fileMenu:    
  126.         //• Mouse down in file menu.
  127.             switch (theItem)
  128.             {
  129.                 case 1:        //• Open file.
  130.                     typeList[0] = 'TEXT';
  131.                     SFGetFile (openP, "\p", 0L, 1, typeList, 0L, &rep);
  132.                     if (rep.good)
  133.                     {
  134.                         //• Doesn't mess with DisableItem, etc.
  135.                         if (fileOpen)
  136.                         {
  137.                             FSClose (fd1);
  138.                             fileOpen = false;
  139.                         }
  140.                         if (FSOpen (rep.fName, rep.vRefNum, &fd1) == noErr)
  141.                         {
  142.                             ScrollRect (&pRect, 0, -11, updateRgn);
  143.                             fileOpen = true;
  144.                         }
  145.                     }
  146.                 break;
  147.     
  148.                 //• Close file.
  149.                 case 3:        
  150.                     if (fileOpen)
  151.                     {
  152.                         FSClose (fd1);
  153.                         fileOpen = FALSE;
  154.                     }
  155.                 break;
  156.         
  157.                 //• Quit.
  158.                 case 5:        
  159.                     if (fileOpen)
  160.                     {
  161.                         FSClose (fd1);
  162.                         fileOpen = FALSE;
  163.                     }
  164.                     doneFlag = 1;
  165.                 break;
  166.             }
  167.         break;
  168.     
  169.         case editMenu:    //• Process system edit events.
  170.             SystemEdit (theItem - 1);
  171.         break;
  172.     }
  173.     HiliteMenu (0);
  174. }
  175.  
  176. //•---------------------------------------------------------------------------*/
  177. void main (void)
  178. {
  179.     short i, j;
  180.     char keyChar;
  181.  
  182.     //• Initialize variables.
  183.  
  184.     j = 0;
  185.     doneFlag = false;
  186.     fileOpen = false;
  187.  
  188.     //• Initialize quickdraw, fonts, events, windows, menus, 
  189.     //• dialogs and cursor.
  190.     InitGraf (&qd.thePort);
  191.     InitFonts ();
  192.     FlushEvents (everyEvent, 0);
  193.     InitWindows ();
  194.     InitMenus ();
  195.     TEInit ();
  196.     InitDialogs (0L);
  197.     InitCursor ();
  198.  
  199.     //• Create an empty region pointer for use by scrollrect later.
  200.     updateRgn = NewRgn ();
  201.  
  202.     //• Setup the menu bar.
  203.     SetUpMenus ();
  204.  
  205.     //• Setup the drag rectangle so part of the window will always 
  206.     //• be visible.
  207.     SetRect (&screenRect, 4, 40, 508, 338);
  208.     SetRect (&dragRect, 4, 24, screenRect.right - 4, 
  209.                                screenRect.bottom - 4);
  210.  
  211.     //• Create the window and set the current port to the window port.
  212.     myWindow = NewWindow (&wRecord, &screenRect, "\pDisplay a file", 
  213.                           true, 0, (WindowPtr)-1L, false, (long) 0);
  214.     SetPort (myWindow);
  215.  
  216.     //• get the rectangle for the current window and put it in pRect.
  217.     BlockMove (&qd.thePort->portRect, &pRect, (long)sizeof pRect);
  218.     howWide = pRect.right  - pRect.left;
  219.  
  220.     //• Now that the window and menus are drawn set the window font 
  221.     //• to monaco 9.
  222.  
  223.     TextFont (monaco);
  224.     TextSize (9);
  225.     MoveTo (pRect.left + 1, pRect.bottom - 2);
  226.  
  227.     //• Main loop to process events.
  228.     do 
  229.     {
  230.         //• If a file is open copy a line to the output window.
  231.         if (fileOpen)
  232.         {
  233.             count = 32;
  234.             FSRead (fd1, &count, tempBuf);
  235.             if (count == 0)
  236.             {
  237.                 FSClose (fd1);
  238.                 fileOpen = false;
  239.                 MoveTo (pRect.left + 1, pRect.bottom - 2);
  240.                 ScrollRect (&pRect, 0, -11, updateRgn);
  241.                 DrawString ("\p-------End of File-------");
  242.                 ScrollRect (&pRect, 0, -11, updateRgn);
  243.                 MoveTo (pRect.left + 1, pRect.bottom - 2);
  244.             }
  245.             else
  246.                 {
  247.                     for (i = 0; i < count; i++)
  248.                     {
  249.                         if (tempBuf[i] > 31)
  250.                             DrawChar (tempBuf[i]);
  251.                         else
  252.                             {
  253.                                 //• Scroll window if we get a carriage 
  254.                                 //• return.
  255.                                 if (tempBuf[i] == '\r')
  256.                                 {
  257.                                     j = 0;
  258.                                     ScrollRect (&pRect, 0, -11, updateRgn);
  259.                                     MoveTo (pRect.left+1, pRect.bottom - 2);
  260.                                 }
  261.                                 //• Expand tabs by outputting spaces.
  262.                                 if (tempBuf[i] == '\011')
  263.                                 {
  264.                                     DrawChar (' ');
  265.                                     j++;
  266.                                     for (; j & 07; j++)
  267.                                     DrawChar (' ');
  268.                                 }
  269.                         }
  270.                     }
  271.             }
  272.         }
  273.         //• Get the next event.
  274.         SystemTask ();
  275.         temp = GetNextEvent (everyEvent, &myEvent);
  276.         switch (myEvent.what)
  277.         {
  278.             //• mouse down, call findwindow to figure out where.
  279.             case mouseDown:  
  280.                 code = FindWindow (myEvent.where, &whichWindow);
  281.                 switch (code)
  282.                 {
  283.                     //• in meun bar, execute the menu command.
  284.                     case inMenuBar:    
  285.                         DoCommand (MenuSelect (myEvent.where));
  286. //                        HandleMenu(MenuSelect(theEvent->where));
  287.                     break;
  288.                     
  289.                     //• in desk accessory, call desk manager.
  290.                     case inSysWindow:    
  291.                         SystemClick (&myEvent, whichWindow);
  292.                     break;
  293.                     
  294.                     //• in drag, call dragwindow to move it.
  295.                     case inDrag:    
  296.                         DragWindow (whichWindow, myEvent.where, &dragRect);
  297.                     break;
  298.                     
  299.                     //• In content area, make application window the 
  300.                     //• frontmost.
  301.                     case inContent:    
  302.                         if (whichWindow != FrontWindow ())
  303.                             SelectWindow (whichWindow);
  304.                     break;
  305.                 }
  306.             break;
  307.         
  308.             //• If keydown event, check for menu command key.
  309.             case keyDown:
  310.                 if (myEvent.modifiers & cmdKey)
  311.                 {
  312.                     keyChar = myEvent.message & charCodeMask;
  313.                     DoCommand (MenuKey (keyChar));
  314.                 }
  315.             break;
  316.         
  317.             case autoKey:
  318.             break;
  319.     
  320.             //• Application window becomming active, do nothing.
  321.             case activateEvt:    
  322.                 if ((myEvent.modifiers & 1) && 
  323.                   (((WindowPtr)myEvent.message) == myWindow))
  324.                 {
  325.                     DisableItem (myMenus[3], 0);
  326.                     EnableItem (myMenus[2], 0);
  327.                     DrawMenuBar ();
  328.                 }
  329.                 else
  330.                     {
  331.                         EnableItem (myMenus[3], 0);
  332.                         DisableItem (myMenus[2], 0);
  333.                         DrawMenuBar ();
  334.                 }
  335.             break;
  336.         
  337.             //• Update event, update the window frame.
  338.             case updateEvt:    
  339.                 if (((WindowPtr)myEvent.message) == myWindow)
  340.                 {
  341.                     BeginUpdate (myWindow);
  342.                     EndUpdate (myWindow);
  343.                 }
  344.                 break;
  345.         }
  346.     } while (doneFlag == 0);
  347. }
  348.  
  349. //•---------------------------------------------------------------------------*/
  350. //• SetUpMenus ()---This subroutine sets up the menu bar and 
  351. //• reads in the desk accessory menu.
  352.  
  353. SetUpMenus ()
  354. {
  355.     short i;
  356.  
  357.     //• Apple menu, \024 is the apple character, adresmenu call 
  358.     //• loads all type DRVR resources.
  359.     myMenus[1] = NewMenu (appleMenu, "\p\024");
  360.     AppendMenu (myMenus[1], "\pAbout \"Display a File\";(-");
  361.     AddResMenu (myMenus[1], 'DRVR');
  362.     
  363.     //• File menu with open, close and quit selections.
  364.     myMenus[2] = NewMenu (fileMenu, "\pFile");
  365.     AppendMenu (myMenus[2], "\pOpen/O;(-;Close/C;(-;Quit/Q");
  366.     
  367.     //• Edit menu with cut, copy and paste.
  368.     myMenus[3] = NewMenu (editMenu, "\pEdit");
  369.     AppendMenu (myMenus[3], "\pUndo;(-;Cut;Copy;Paste");
  370.     AppendMenu (myMenus[3], "\pClear;(-;Show Clipboard");
  371.     
  372.     //• Stop scroll menu.
  373.     myMenus[4] = NewMenu (stopMenu, "\pClick Here to Pause Printout");
  374.     AppendMenu (myMenus[4], "\pRelease mouse button to resume printout");
  375.     for (i = 1; i <= lastMenu; i++)
  376.         InsertMenu (myMenus[i], 0);
  377.     
  378.     //• Draw the completed menu bar.
  379.     DrawMenuBar ();
  380. }
  381.  
  382. //•---------------------------------------------------------------------------*/
  383. //• Center a string in the window.
  384.  
  385. void Center (char *str)
  386. {
  387.     Move (((howWide - StringWidth ((StringPtr) str)) / 2), 0);
  388.     DrawString ((StringPtr) str);
  389.     Move (-(myWindow->pnLoc.h), (myWindow->txSize) + 2);
  390. }
  391.  
  392. //•---------------------------------------------------------------------------*/
  393. //• Draw a Pretty design.
  394.  
  395. Pretty ()
  396. {
  397.     short j;
  398.     Rect tmpRec;
  399.  
  400.     BlockMove (&pRect, &tmpRec, (long)sizeof pRect);
  401.     for (j = 0;j < 12; j++)
  402.     {
  403.         FrameOval (&tmpRec);
  404.         InsetRect (&tmpRec, 6, 0);
  405.     }
  406.     BlockMove (&pRect, &tmpRec, (long)sizeof pRect);
  407.     for (j = 0;j < 9; j++)
  408.     {
  409.         FrameOval (&tmpRec);
  410.         InsetRect (&tmpRec, 0, 6);
  411.     }
  412. }
  413.  
  414.  
  415.